package com.kt.NetworkModel.utils;

import android.app.Activity;
import android.content.Context;

import com.kaopiz.kprogresshud.KProgressHUD;

/**
 * admin
 * 2018/12/26
 * content :
 */
public class LoadingNetUtils {
    private static LoadingNetUtils mInstance;
    private KProgressHUD netDialog;

    public static LoadingNetUtils getInstance() {
        if (mInstance == null) {
            synchronized (LoadingNetUtils.class) {
                if (mInstance == null) {
                    mInstance = new LoadingNetUtils();
                }
            }
        }
        return mInstance;
    }

    private LoadingNetUtils() {
    }

    public void showLoading(Context context) {
        try {
            if (context instanceof Activity) {
                Activity activity = (Activity) context;
                if (activity.isDestroyed() || activity.isFinishing()) {
                    return;
                }
            }
            if (netDialog == null) {
                netDialog = new KProgressHUD(context);
            }
            netDialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showLoading(Activity context, boolean isCancellable) {
        try {
            if (context.isDestroyed() || context.isFinishing()) {
                return;
            }
            if (netDialog == null) {
                netDialog = new KProgressHUD(context);
            }
            netDialog.setCancellable(isCancellable);
            netDialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void dismissLoading() {
        try {
            if (netDialog != null) {
                try {
                    netDialog.dismiss();
                    netDialog = null;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
